package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportPurchaseOut;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.TransportPurchaseOutDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2022-03-05 09:16:16
 */
public class TransportPurchaseOutConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportPurchaseOutConverts.class);

    public static List<TransportPurchaseOutDTO> convert(List<TransportPurchaseOut> transportPurchaseOutList) {
        if (CollectionUtils.isEmpty(transportPurchaseOutList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportPurchaseOutDTO> transportPurchaseOutListResult = Lists.newArrayList();
        for (TransportPurchaseOut transportPurchaseOut : transportPurchaseOutList) {
            transportPurchaseOutListResult.add(convert(transportPurchaseOut));
        }
        return transportPurchaseOutListResult;
    }

    public static TransportPurchaseOutDTO convert(TransportPurchaseOut transportPurchaseOut) {
        try {
            return BeanUtils.copy(transportPurchaseOut, TransportPurchaseOutDTO.class);
        } catch (Exception e) {
            LOG.error("TransportPurchaseOut到TransportPurchaseOutDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportPurchaseOut到TransportPurchaseOutDTO转换失败", e);
        }
    }

    public static TransportPurchaseOut convert(TransportPurchaseOutDTO transportPurchaseOutDTO) {
        try {
            return BeanUtils.copy(transportPurchaseOutDTO, TransportPurchaseOut.class);
        } catch (Exception e) {
            LOG.error("TransportPurchaseOutDTO到TransportPurchaseOut转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportPurchaseOutDTO到TransportPurchaseOut转换失败", e);
        }
    }
}